package com.rd.student.jike.class2.lihongming.lock718;

/**
 * @Auther:Heinrich
 * @Date:2023/7/18-07-18-16:49
 * @Description:c230718
 * @version:1.0
 */
public class TestBank extends Thread{


    //账户来取钱，构成类的组合关系


    Account1 account;
    double money;
    public TestBank(String name, Account1 account, double money) {
        super(name);
        this.account = account;
        //System.out.println(account.getBalance()+" "+getName());
        this.money = money;
    }
    //取钱
    @Override
    public void run() {
            account.drawMoney(account,money);
        }

    public static void main(String[] args) throws InterruptedException {
        Account1 account1 = new Account1("123456",888);
        new TestBank("张三",account1,666).start();
        new TestBank("李四",account1,666).start();
        new TestBank("王五",account1,666).start();
        Thread.sleep(2000);
        System.out.println(account1.getBalance());
    }
}
class Account1{
     private String account0ID;
     private double balance;

    public Account1() {
    }

    public Account1(String account0ID, double balance) {
        this.account0ID = account0ID;
        this.balance = balance;
    }
    public synchronized void drawMoney(Account1 account,double money){
        if(account.getBalance()-money<0){
            System.out.println(Thread.currentThread().getName()+" 不能取钱，余额不足");
        }else{
            System.out.println(Thread.currentThread().getName()+" 可以取钱");
            account.setBalance(account.getBalance()-money);
            System.out.println("取钱后的余额为："+account.getBalance());
        }
    }
    public String getAccount0ID() {
        return account0ID;
    }

    public void setAccount0ID(String account0ID) {
        this.account0ID = account0ID;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}
